package team.epos.merchant.servlet;


import team.epos.merchant.dto.ResponseBody;
import team.epos.merchant.service.LoginService;
import team.epos.merchant.service.LoginServiceImpl;
import team.epos.merchant.service.StoreServiceImpl;
import team.epos.utils.annotation.RequestMethod;
import team.epos.utils.enums.StoreState;
import team.epos.utils.pojo.MUser;
import team.epos.utils.pojo.Store;
import team.epos.utils.servlet.BaseServlet;
import team.epos.utils.util.JsonUtil;
import team.epos.utils.util.PojoParamUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author CHENKI
 */
public class LoginServlet extends BaseServlet {

    private LoginService loginService = new LoginServiceImpl();
    private StoreServiceImpl storeService = new StoreServiceImpl();

    @RequestMethod(method = "login")
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        //维持会话
        HttpSession session = req.getSession();
        MUser user = PojoParamUtil.getObject(req, MUser.class);
        MUser resUser = loginService.findMuser(user);
        String state = "";
        try {
            if (resUser != null) {
                session.setAttribute("muser", resUser);
//                TODO：判断是否开店
//                sid为0，则为没申请过开店，需要跳转到申请jsp，
                if (resUser.getsId() == 0) {
//                    req.setAttribute("store", new Store());
//                    req.getRequestDispatcher("/jsp/store.html").forward(req, resp);
                    String json = JsonUtil.obJectToJson(new ResponseBody(StoreState.NO_APPLY.getCode(), resUser.getsId()));
                    resp.getWriter().write(json);
                } else {
//                    sid不为0，则已经申请过开店，再请求该商店的状态，判断当前状态，
                    state = storeService.getState(resUser.getsId());
                    stateCallback(state, resp, req, resUser.getsId());
                }

            } else {
                resp.getWriter().write("0");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stateCallback(String state, HttpServletResponse resp, HttpServletRequest req, int sId) throws Exception {
        int sta = Integer.parseInt(state);
        String json = JsonUtil.obJectToJson(new ResponseBody(sta, sId));
        resp.getWriter().write(json);
//        if (sta == StoreState.WAIT_CHECK.getCode()) {
//            resp.getWriter().write("您的开店申请在待审核状态！");
//        } else if (sta == StoreState.PASS.getCode()) {
//
////            req.getRequestDispatcher("/dishes?method=findAllDishes&sId=" + sId).forward(req, resp);
//        } else if (sta == StoreState.NO_PASS.getCode()) {
//            resp.getWriter().write("您没有权限！");
//        } else if (sta == StoreState.REJECT.getCode()) {
//            Store store = storeService.rejectApply(sId);
//            req.setAttribute("store", store);
//            req.getRequestDispatcher("/jsp/store.html").forward(req, resp);
//        } else {
//            resp.getWriter().write("您的开店申请在待审核状态！");
//        }

    }
}
